package com.nuoniu.sibanyun.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.config.JwtUtils;
import com.nuoniu.sibanyun.entity.SysUser;
import com.nuoniu.sibanyun.entity.dto.SysUserDto;
import com.nuoniu.sibanyun.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@RestController
@RequestMapping("/sysUser")
@Api(value = "用户管理", tags = "用户管理")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private JwtUtils jwtUtils;

    @ApiOperation("获取用户详情")
    @GetMapping("getUserInfo")
    public HttpResult getUserInfo(HttpServletRequest request){
        return HttpResult.ok(sysUserService.getUserSimpleVo(request));
    }

    /**
     * 分页获取用户列表
     * @author 小啊刚
     * @date 2020/6/2
     */
    @ApiOperation("分页获取用户列表")
    @GetMapping("/user/getPageList")
    public HttpResult getPageList(SysUserDto userDto) {
        return HttpResult.ok(sysUserService.getPageList(userDto));
    }

    /**
     * 分页获取用户列表
     * @author 小啊刚
     * @date 2020/6/2
     */
    @ApiOperation("分页获取用户列表")
    @GetMapping("/user/page")
    public HttpResult getUserPage(SysUserDto userDto) {
        return HttpResult.ok(sysUserService.getUserPage(userDto));
    }

    /**
     * 保存用户
     * @author 小啊刚
     * @date 2020/6/2
     */
    @ApiOperation("保存用户")
    @PostMapping("/user/save")
    public HttpResult saveUser(@RequestBody SysUserDto sysUser) {
        if(sysUser.getId() != null && sysUser.getStatus() != null && StringUtils.isBlank(sysUser.getUserName())) {
            SysUser user = new SysUser();
            user.setStatus(sysUser.getStatus());
            user.setId(sysUser.getId());
            try {
                //修改 用户状态 清除当前 用户的token 强制用户重新登陆
                SysUser findUser = sysUserService.getById(sysUser.getId());
                jwtUtils.clearUser(findUser.getUserName());
            }catch (Exception e){

            }
            return sysUserService.updateUser(user);
        }
        sysUserService.saveUser(sysUser);
        return HttpResult.ok();
    }

    /**
     * 删除用户
     * @author 小啊刚
     * @date 2020/6/3
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/delete")
    public HttpResult delete(Long id) {
        sysUserService.delete(id);
        return HttpResult.ok();
    }

    /**
     * 重置密码
     * @author 小啊刚
     * @date 2020/6/4
     */
    @ApiOperation("重置密码")
    @PostMapping("/user/resetPassword")
    public HttpResult resetPassword(@RequestBody SysUserDto sysUser) {
        sysUserService.resetPassword(sysUser);
        return HttpResult.ok();
    }

    /**
     * 更换管理员
     * @author 小啊刚
     * @date 2020/6/4
     */
    @ApiOperation("更换管理员")
    @GetMapping("/user/updateSysManager")
    public HttpResult updateSysManager(Long userId) {
        if(userId == null) {
            throw new NuoNiuException("请选择用户");
        }

        SysUser user = sysUserService.getById(userId);
        if(user != null && user.getStatus() != 0) {
            throw new NuoNiuException("用户状态'停用',启用后更换");
        }

        sysUserService.updateSysManager(userId);
        return HttpResult.ok();
    }

    /**
     * 分配角色
     * @author 小啊刚
     * @date 2020/6/6
     */
    @ApiOperation("分配角色")
    @PostMapping("/user/saveUserRole")
    public HttpResult saveUserRole(@RequestBody SysUserDto sysUser) {
        sysUserService.saveUserRole(sysUser.getRoleId(), sysUser.getId());
        return HttpResult.ok();
    }

    /**
     * 获取 当前用户 的数据权限用户
     */
    @ApiOperation("获取当前用户的数据权限用户")
    @GetMapping("/user/getDataScopeUsers")
    public HttpResult getDataScopeUsers() {
        return HttpResult.ok(sysUserService.getDataScopeUsers());
    }

    /**
     * 个人䄴户列表
     * @author 小啊刚
     * @date 2020-08-25
     */
    @ApiOperation("个人䄴户列表")
    @GetMapping("/user/pageMember")
    public HttpResult pageMember(Page page, SysUser user) {
        return HttpResult.ok(sysUserService.pageMember(page, user));
    }

    @ApiOperation("获取详情")
    @GetMapping("getDetail")
    public HttpResult getDetail(Long id) {
        return HttpResult.ok(sysUserService.getById(id));
    }


}
